package com.example.footballsystem.service;

import com.example.footballsystem.entity.FieldPosition;
import com.example.footballsystem.entity.Field;
import com.example.footballsystem.mapper.FieldMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class FieldService {
    @Autowired
    FieldMapper filedMapper;

    //获取全部预定信息
    public List<Field> getAllField(){
        return filedMapper.getAllField();
    }
    //搜索场地
    public List<Field> getField(String info){
        List<Field> resultList = new ArrayList<>();
        int count = 0;

        if(!filedMapper.getFieldByAdress(info).isEmpty()){
            resultList.addAll((filedMapper.getFieldByAdress(info)));
            count++;
        }
        try{
            if(!filedMapper.getFieldByID(Integer.parseInt(info)).isEmpty()){
                resultList.addAll((filedMapper.getFieldByID(Integer.parseInt(info))));
                count++;
            }
        }catch (NumberFormatException e) {
            if (!filedMapper.getFieldByMasterAccount(info).isEmpty()) {
                resultList.addAll((filedMapper.getFieldByMasterAccount(info)));
                count++;
            }
            for (int i = 0; i < count; i++) {
                for (int j = i + 1; j < count; j++) {
                    if (Objects.equals(resultList.get(j).getFieldID(), resultList.get(j).getFieldID())) {
                        log.info(i + " " + j);
                        resultList.remove(j);
                        j--;
                        count--;
                    }
                }
            }
            //log.info(resultList.toString());
            return resultList;
        }
        if(!filedMapper.getFieldByMasterAccount(info).isEmpty()){
            resultList.addAll((filedMapper.getFieldByMasterAccount(info)));
            count++;
        }
        for (int i=0;i<count;i++) {
            for (int j=i+1;j<count;j++) {
                if(Objects.equals(resultList.get(j).getFieldID(), resultList.get(j).getFieldID())){
                    log.info(i+" "+j);
                    resultList.remove(j);
                    j--;
                    count--;
                }
            }
        }
        //log.info(resultList.toString());
        return resultList;
    }
    //获取场地经纬度
    public FieldPosition getPosition(int id){
        FieldPosition position = new FieldPosition();
        position.setLatitude(filedMapper.getLatitude(id));
        position.getLongitude(filedMapper.getLongitude(id));
        return position;
    }
    //添加场地
    public Boolean addField(Field field){
        Timestamp time = new Timestamp(System.currentTimeMillis());
        field.setCreateDate(time.toString());
        return filedMapper.addField(field);
    }
    //删除场地
    public Boolean deleteField(Field field){
        return filedMapper.deleteField(field.getFieldID());
    }
}
